package com.cloudcentral.controller;

import com.cloudcentral.entity.Detect;
import com.cloudcentral.entity.Radio;
import com.cloudcentral.entity.dto.DetectActionDTO;
import com.cloudcentral.entity.query.DetectQuery;
import com.cloudcentral.entity.vo.DetectDetailVO;
import com.cloudcentral.entity.vo.PageVO;
import com.cloudcentral.enums.RadioStatus;
import com.cloudcentral.exception.FailureException;
import com.cloudcentral.service.DetectService;
import com.cloudcentral.util.Result;
import com.cloudcentral.websocket.RadioWebSocket;
import com.cloudcentral.websocket.entity.WSMessage;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 探测任务任务表 前端控制器
 * </p>
 *
 * @author Administrator
 * @since 2023-11-23
 */
@Slf4j
@RestController
@RequestMapping("/detect")
@Tag(name = "探测任务")
public class DetectController extends BaseController {
  private final DetectService detectService;

  @Autowired
  public DetectController(DetectService detectService) {
    this.detectService = detectService;
  }

  @PostMapping("/action")
  @Operation(summary = "启动/停止")
  public Result<Boolean> action(DetectActionDTO dto) throws FailureException {
    DetectDetailVO detectDetailVO = detectService.selectDetailById(dto.getDetectId());
    WSMessage<?> wsMessage;
    if (dto.getStatus()) wsMessage = WSMessage.ok(WSMessage.WSMessageType.START_DETECT, detectDetailVO);
    else wsMessage = WSMessage.ok(WSMessage.WSMessageType.STOP_DETECT, dto.getDetectId());
    if (dto.getStatus()) {
      List<Radio> radioList = new ArrayList<>();
      radioList.add(detectDetailVO.getMaster());
      radioList.add(detectDetailVO.getSlave());
      radioList.addAll(detectDetailVO.getListeners());
      for (Radio radio : radioList) {
        if (RadioStatus.value(radio.getId()).equals(RadioStatus.OFFLINE)) {
          throw new FailureException(String.format("台站\"%s\"处于离线状态，无法启动", radio.getName()));
        } else if (!RadioStatus.value(radio.getId()).equals(RadioStatus.IDLE)) {
          throw new FailureException(String.format("台站\"%s\"处于任务中，无法启动", radio.getName()));
        }
      }
    }
    RadioWebSocket.sendMessageById(detectDetailVO.getMaster().getId(), wsMessage);
    RadioWebSocket.sendMessageById(detectDetailVO.getSlave().getId(), wsMessage);
    for (Radio listener : detectDetailVO.getListeners()) RadioWebSocket.sendMessageById(listener.getId(), wsMessage);
    return Result.ok(true);
  }

  @PostMapping("/submit")
  @Operation(summary = "提交")
  public Result<Boolean> submit(Detect detect) throws FailureException {
    return Result.ok(detectService.submit(detect));
  }

  @DeleteMapping("/delete")
  @Operation(summary = "删除")
  public Result<Boolean> delete(Long id) throws FailureException {
    return Result.ok(detectService.delete(id));
  }

  @GetMapping("/listPage")
  @Operation(summary = "分页查询")
  public Result<PageVO<DetectDetailVO>> listPage(DetectQuery query) {
    return Result.ok(detectService.listPage(query));
  }
}
